package com.kaikai.edu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.kaikai.commonutils.R;
import com.kaikai.edu.entity.EduCourse;
import com.kaikai.edu.entity.EduSubject;
import com.kaikai.edu.entity.ExcelSubjectData;
import com.kaikai.edu.entity.subject.OneSubject;
import com.kaikai.edu.entity.subject.TwoSubject;
import com.kaikai.edu.listener.ExcelListener;
import com.kaikai.edu.mapper.EduSubjectMapper;
import com.kaikai.edu.service.EduCourseService;
import com.kaikai.edu.service.EduSubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author yujiankai
 * @since 2021-05-13
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Autowired
    private EduSubjectMapper subjectMapper;

    @Autowired
    private EduCourseService courseService;

    @Override
    public void importSubjectData(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), ExcelSubjectData.class, new ExcelListener(this)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<OneSubject> getOneTwoSubject() {
        //用于最后存储的集合
        List<OneSubject> oneSubjectList = new ArrayList<>();


        //查询一级分类
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        //select * from edu_subject where parent_id='0'
        wrapper.eq("parent_id", 0);
        List<EduSubject> listOne = this.list(wrapper);


        //查询所有二级分类
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id", 0);
        List<EduSubject> listTwo = this.list(wrapperTwo);

        //封装分类
        for (EduSubject one : listOne) {
            //封装一级分类

            OneSubject oneSubject = new OneSubject();
            BeanUtils.copyProperties(one, oneSubject);
            oneSubject.setIsShow(false);

            //用于存储每个一级分类中的二级分类集的集合
            List<TwoSubject> twoSubjectList = new ArrayList<>();
            //遍历封装二级分类
            for (EduSubject two : listTwo) {
                if (two.getParentId().equals(one.getId())) {
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(two, twoSubject);
                    twoSubject.setIsShow(false);
                    twoSubjectList.add(twoSubject);
                }
            }
            oneSubject.setChildren(twoSubjectList);
            oneSubjectList.add(oneSubject);
        }
        return oneSubjectList;
    }

    @Override
    public R deleteSubjectById(String id) {
        try {
            QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
            wrapper.eq("subject_id", id);
            List<EduCourse> courseList = courseService.list(wrapper);
            if (!CollectionUtils.isEmpty(courseList)) {
                return R.error().message("该分类下存在课程，不可删除");
            }
            int i = subjectMapper.deleteById(id);
            if(i == 1){
                return R.ok().message("删除成功");
            }
            return R.error().message("删除失败");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error().message("删除失败");
        }
    }

    @Override
    public R updateSubject(EduSubject subject) {
        try {
            subject.setGmtModified(new Date());
            UpdateWrapper<EduSubject> wrapper = new UpdateWrapper();
            wrapper.eq("id", subject.getId());
            boolean update = this.update(subject, wrapper);
            if(update){
                return R.ok().message("更新成功");
            }else{
                return R.error().message("更新失败");
            }

        }catch(Exception e){
            e.printStackTrace();
            return R.error().message("更新失败");
        }
    }

    @Override
    public R createSubject(EduSubject subject) {
        try {
            subject.setGmtCreate(new Date());
            subjectMapper.insert(subject);
            return R.ok().message("添加成功");
        }catch(Exception e){
            e.printStackTrace();
            return R.error().message("添加失败");
        }
    }
}
